<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" id="viewport"
	content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<link href="./css/reset.css" rel="stylesheet" type="text/css">
<link href="./css/shoping-cart.css" rel="stylesheet" type="text/css">
</head>
<body style="background: #F5F5F4;">
	<div class="near-box">
		<div class="header">
			<a href="javascript:history.back()" class="left"></a>
			<p class="headerText">购物车</p>
			<span class="shop-cart-htext1">编辑</span>
		</div>
		<div class="shop-cart-bigbox">
			<!--  <div class="cart-title"></div> -->
			<!-- <div class="cart-title"> <i>今天30分钟可送达(可预订)</i></div> -->
			<div class="shop-cart-listbox1">
				<div class="shop-cart-box2">
					<input type="checkbox" name="sub1" class="btn1"> <span
						class="shop-cart-ltext1">天祥瑞自营商品</span>
				</div>
			</div>
			<!-- 后台购物车查询结束 -->
			<div class="shop-cart-total">
				<label class="checkall"> <span class="shop-cart-check1"><input
						type="checkbox" class="" id="ckAll"></span> 全选
				</label> <span class="scart-total-text2">合计：￥</span> <span id="AllTotal"
					class="scart-total-text3">0.00</span> <a href="javascript:pay()"
					class="scart-total-text4">去结算<i id="selectedTotal"></i></a> <span
					class="delete hide"><a href="javascript:delteShop()"></a></span>
			</div>
			<div class="kaola-bottom">
				<a href="./index.jsp" class="kaola-bottom-box1"
					style="background-color: red;"> <span class="kaola-bottom-img1"><img
						src="./menu/1556015694.png"></span> <span
					class="kaola-bottom-text1 text2" style="color: #f9f8f8;">首页</span>
				</a> <a href="./online-msg.jsp" class="kaola-bottom-box1"
					style="background-color: red;"> <span class="kaola-bottom-img1"><img
						src="./menu/eec3b3a1137ca3a7d155638019db8c2.png"></span> <span
					class="kaola-bottom-text1" style="color: #f9f8f8;">留言</span>
				</a> <a href="./shoping-cart.jsp" class="kaola-bottom-box1"> <span
					class="kaola-bottom-img1"><img src="./menu/1556015745.png"></span>
					<span class="kaola-bottom-text1" style="color: red;">购物车</span>
				</a> <a href="./personal-center.jsp" class="kaola-bottom-box1"
					style="background-color: red;"> <span class="kaola-bottom-img1"><img
						src="./menu/wode.png"></span> <span class="kaola-bottom-text1"
					style="color: #f9f8f8;">我的</span>
				</a>
			</div>
		</div>
	</div>
	<input type="hidden" id="ids" value="">
	<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>

	<script type="text/javascript">
$(function(){
	window.alert = function(name) {
		var iframe = document.createElement("IFRAME");
		iframe.style.display = "none";
		iframe.setAttribute("src", 'data:text/plain,');
		document.documentElement.appendChild(iframe);
		window.frames[0].window.alert(name);
		iframe.parentNode.removeChild(iframe);
	}
	$.post("../../../tszs/txr/findUidCar.do",function(data){
		var hmtl="";
		var path="<%=request.getSession().getAttribute("path")%>";
		
								for (var i = 0; i < data.data.length; i++) {
									hmtl += "<div class='index-goods'>"
											+ "<span class='shop-cart-check2'>"
											+ "<input  type='checkbox' name='sub2' class='shopcart-input1 btn2' value='"+data.data[i].p_id+"'></span>"
											+ "<span class='index-goods-img'><a href=\"javascript:findShopDatails('"
											+ data.data[i].product_id
											+ "')\"><img src='/file/"+data.data[i].phtoto+"'></span></a>"
											+ "<div class='index-goods-textbox'>"
											+ "<span class='index-goods-text1'>"
											+ data.data[i].c_name
											+ "</span>"
											+ "<span class='index-goods-text1'>"
											+ data.data[i].c_describe
											+ "</span>"
											+ "<div class='index-goods-text2'>￥<i class='priceJs'>"
											+ data.data[i].u_name
											+ "</i></div>"
											+ "<div class='shop-cart-box3'>"
											+ "<span id='"+data.data[i].p_id+"' class='shop-cart-subtract'></span>"
											+ "<input type='tel' size='4' value='"+data.data[i].product_num+"' id='"+data.data[i].p_id+"' class='shop-cart-numer'>"
											+ "<span id='"+data.data[i].p_id+"' class='shop-cart-add'></span>"
											+ "</div>" + "</div>" + "</div>";
								}
								var subhtml = hmtl
										+ "<div class='shopPrice'>本店总计：￥<span class='ShopTotal'>0.00</span></div>";
								$(".shop-cart-listbox1").append(subhtml);
							}, "json")
		})
	</script>
	<script type="text/javascript">
		$(".shop-cart-listbox1").on("click", '.shop-cart-add', function() {
			var multi = 0;
			var obj = $(this).prev();
			console.log("prev");
			console.log(obj);
			var vall = obj.val();
			vall++;
			$.post("../../../tszs/txr/updateShopCar.do?", {
				ProductNum : vall,
				PId : $(this).context.id
			}, function(data) {
				if (data.type == 1) {
					obj.val(vall);
					TotalPrice();
				}
			}, "json");
		});
		$(".shop-cart-listbox1").on("click", '.shop-cart-subtract', function() {
			var multi = 0;
			var obj = $(this).next();
			console.log("next")
			console.log(obj)
			var vall = obj.val();
			vall--;
			if (vall <= 0) {
				vall = 1;
			}
			$.post("../../../tszs/txr/updateShopCar.do?", {
				ProductNum : vall,
				PId : $(this).context.id
			}, function(data) {
				if (data.type == 1) {
					obj.val(vall);
					TotalPrice();
				}
			}, "json")
		});

		$(".btn1").on(
				"click",
				function() {
					var $btn2 = $(this).parent(".shop-cart-box2").siblings(
							".index-goods").children(".shop-cart-check2")
							.children(".btn2");
					if ($(this).is(':checked')) {
						$btn2.prop("checked", this.checked);
						TotalPrice();
					} else {
						$btn2.removeAttr("checked");
						TotalPrice();
					}
				});

		$(".shop-cart-listbox1").on(
				"click",
				".btn2",
				function() {
					var goods = $(this).closest(".shop-cart-listbox1").find(
							".btn2"); //获取本店铺的所有商品
					var goodsC = $(this).closest(".shop-cart-listbox1").find(
							".btn2:checked"); //获取本店铺所有被选中的商品
					var Shops = $(this).closest(".shop-cart-listbox1").find(
							".btn1"); //获取本店铺的全选按钮
					if (goods.length == goodsC.length) { //如果选中的商品等于所有商品
						Shops.prop('checked', true); //店铺全选按钮被选中
						TotalPrice();
					} else { //如果选中的商品不等于所有商品
						Shops.prop('checked', false); //店铺全选按钮不被选中
						TotalPrice();
					}
				});

		$("#ckAll").on("click", function() {
			$("input[name='sub2']").prop("checked", this.checked);
			var arr = new Array();
			$("input:checkbox[name='sub2']:checked").each(function(i) {
				arr[i] = $(this).val();
			});
			var b = arr.join(",");
			TotalPrice();
		});
		$("input[name='sub2']")
				.click(
						function() {
							var $subs = $("input[name='sub2']");
							$("#ckAll")
									.prop(
											"checked",
											$subs.length == $subs
													.filter(":checked").length ? true
													: false);

							TotalPrice();
						});

		$(".shop-cart-htext1").click(function() {
			$(".shop-cart-htext1").html("取消");
			$(".scart-total-text2").toggleClass("hide");
			$(".scart-total-text3").toggleClass("hide");
			$(".scart-total-text4").toggleClass("hide");
			$(".delete").toggleClass("hide");
			TotalPrice();
		});

		$(".delete").click(
				function() {
					var arr = new Array();
					$("input:checkbox[name='sub2']:checked").each(function(i) {
						arr[i] = $(this).val();
					});
					var pId = arr.join(",");
					if ($("#ckAll").is(':checked')) {
						$(".shop-cart-listbox1").remove();
						$("#ckAll").prop('checked', false);
						TotalPrice();
					}
					if ($(".btn1").is(':checked')) {
						$(".btn1:checked").closest(".shop-cart-listbox1")
								.remove();
						TotalPrice();
					}
					if ($(".btn2").is(':checked')) {
						$(".btn2:checked").parent(".shop-cart-check2").parent(
								".index-goods").remove();
						TotalPrice();
					}

					if (pId == "") {
						alert("请选择一些商品再试吧！")
						return;
					}

					var isTrue = confirm("是否确认删除？")
					if (isTrue) {
						$.post("../../../tszs/txr/updatePstatic.do?pId=" + pId,
								function(data) {
									if (data.type == 1) {
										window.location.href = "";
									}
								}, "json")
					} else {
						window.location.href = "";
					}
				});

		function TotalPrice() {
			var allprice = 0; //总价
			$(".shop-cart-listbox1").each(
					function() { //循环每个店铺
						var oprice = 0; //店铺总价
						$(this).find(".btn2").each(
								function() { //循环店铺里面的商品
									if ($(this).is(":checked")) { //如果该商品被选中
										var num = $(this).parents(
												".index-goods").find(
												".shop-cart-numer").val(); //得到商品的数量
										var price = parseFloat($(this).parents(
												".index-goods")
												.find(".priceJs").text()); //得到商品的单价
										var total = price * num; //计算单个商品的总价
										oprice += total; //计算该店铺的总价
									}
									$(this).closest(".shop-cart-listbox1")
											.find(".ShopTotal").text(
													oprice.toFixed(2)); //显示被选中商品的店铺总价
								});
						var oneprice = parseFloat($(this).find(".ShopTotal")
								.text()); //得到每个店铺的总价
						allprice += oneprice; //计算所有店铺的总价
					});
			$("#AllTotal").text(allprice.toFixed(2)); //输出全部总价
		}
		function pay() {
			var arr = new Array();
			$("input:checkbox[name='sub2']:checked").each(function(i) {
				arr[i] = $(this).val();
			});
			var b = arr.join(",");
			if (b == "") {
				alert("请勾选商品")
				return;
			} else if (b == "on") {
				alert("请勾选商品")
				return;
			} else {
				window.location.href = "indent-details.jsp?cid=" + b;
			}
		}
		function findShopDatails(cid) {
			window.location.href = "goods-details.jsp?cid=" + cid;
		}
	</script>
</body>
</html>